package com.dcrzl.shcz.module.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.shcz.common.entity.info.Gov;
import com.dcrzl.shcz.common.entity.info.GovUnit;
import com.dcrzl.shcz.common.entity.info.MiniUserUnit;
import com.dcrzl.shcz.common.mapper.info.MiniUserUnitMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.info.service.GovService;
import com.dcrzl.shcz.module.info.service.GovUnitService;
import com.dcrzl.shcz.module.info.service.MiniUserUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

@Service("miniUserUnitService")
public class MiniUserUnitServiceImpl extends BaseServiceImpl<MiniUserUnitMapper, MiniUserUnit> implements MiniUserUnitService {

    @Autowired
    private GovUnitService govUnitService;

    @Autowired
    private GovService govService;

    private LambdaQueryWrapper<MiniUserUnit> commonWrapper(MiniUserUnit miniUserUnit) {
        LambdaQueryWrapper<MiniUserUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(miniUserUnit.getUserId()),MiniUserUnit::getUserId,miniUserUnit.getUserId());
        wrapper.eq(Objects.nonNull(miniUserUnit.getGovUnitId()),MiniUserUnit::getGovUnitId,miniUserUnit.getGovUnitId());
        wrapper.in(!CollectionUtils.isEmpty(miniUserUnit.getUserIdList()),MiniUserUnit::getUserId,miniUserUnit.getUserIdList());
        wrapper.eq(Objects.nonNull(miniUserUnit.getGovType()),MiniUserUnit::getGovType,miniUserUnit.getGovType());
        return wrapper;
    }

    @Override
    public Result<MiniUserUnit> selectOne(MiniUserUnit miniUserUnit) {
        LambdaQueryWrapper<MiniUserUnit> wrapper = this.commonWrapper(miniUserUnit);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MiniUserUnit>> selectList(MiniUserUnit miniUserUnit) {
        LambdaQueryWrapper<MiniUserUnit> wrapper = this.commonWrapper(miniUserUnit);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MiniUserUnit> selectPage(MiniUserUnit miniUserUnit) {
        LambdaQueryWrapper<MiniUserUnit> wrapper = this.commonWrapper(miniUserUnit);
        return super.selectPage(miniUserUnit, wrapper);
    }

    @Override
    public Result<List<MiniUserUnit>> selectUList(MiniUserUnit miniUserUnit) {
        Result<List<MiniUserUnit>> listResult = this.selectList(miniUserUnit);
        if(listResult.isSuccess()){
            List<MiniUserUnit> list = listResult.getData();
            Set<Long> govTypeList = new HashSet<>();
            Set<Long> govUnitIdList = new HashSet<>();
            list.forEach(miniUserUnit1 -> {
                govTypeList.add(miniUserUnit1.getGovType());
                govUnitIdList.add(miniUserUnit1.getGovUnitId());
            });
            Result<List<Gov>> listResult1 = govService.selectList(new Gov().setIdList(govTypeList));
            if(listResult1.isSuccess()){
                List<Gov> data = listResult1.getData();
                Map<Long,Gov> map = new HashMap<>();
                data.forEach(gov -> {
                    map.put(gov.getId(),gov);
                });
                list.forEach(miniUserUnit1 -> {
                    miniUserUnit1.setGov(map.get(miniUserUnit1.getGovType()));
                });
            }
            Result<List<GovUnit>> listResult2 = govUnitService.selectList(new GovUnit().setIdList(govUnitIdList));
            if(listResult2.isSuccess()){
                List<GovUnit> data = listResult2.getData();
                Map<Long,GovUnit> map = new HashMap<>();
                data.forEach(govUnit -> {
                    map.put(govUnit.getId(),govUnit);
                });
                list.forEach(miniUserUnit1 -> {
                    miniUserUnit1.setGovUnit(map.get(miniUserUnit1.getGovUnitId()));
                });
            }
        }
        return listResult;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MiniUserUnit miniUserUnit = new MiniUserUnit().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(miniUserUnit);
    }

}